#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/6/5 20:09
# @Author : 闫峰
# @File    : auth.py
import json

import jsonpath
import requests

from common.setting import ensure_path_sep
from utils.readFileUtils.yamlControl import GetYamlData


def xxljob_cookie():
    login_url = "https://whaleapi.internal.zhcxkj.com/xxl-job-admin/login"
    # 表单传值
    login_head = {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "Connection": "keep-alive"}
    # 登录的参数
    login_body = {"userName": "admin", "password": "123456"}

    # post请求 进行登录
    res = requests.post(url=login_url, headers=login_head, data=login_body, allow_redirects=False)
    cookies = res.cookies
    # 打印cookies
    # print(cookies)
    return_cookie = requests.utils.dict_from_cookiejar(cookies)['XXL_JOB_LOGIN_IDENTITY']
    return return_cookie


def shopeev2_login_init():
    login_data = GetYamlData(file_dir=ensure_path_sep("\\config\\javaShopeev2Login.yaml")).get_yaml_data()  # dict
    # 调账号验证接口获取token
    res = requests.post(url=login_data['url'], json=login_data['data'], headers=login_data['headers'])
    # 调重定向接口获取cookie
    url2 = login_data['url2'] + res.json()['message']
    res2 = requests.get(url=url2, headers=login_data['headers'], allow_redirects=False)
    accessToken = jsonpath.jsonpath(json.loads(res2.text),"$..accessToken")[0]
    return accessToken

shopeev2_login_init()